package com.greateffect.littlebud.lib.itemdecoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by ZengCS on 2019/4/8.
 * function：自定义使用GridLayout的间距
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int column;
    private int space;
    private boolean excludeFirstSpace;//去除第一列左侧空间

    public SpaceItemDecoration(int space,int column) {
        this(space,column,true);
    }

    public SpaceItemDecoration(int space,int column,boolean excludeFirstSpace){
        this.space = space;
        this.column = column;
        this.excludeFirstSpace = excludeFirstSpace;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //不是第一个的格子都设一个左边和底部的间距
        outRect.left = space;
        outRect.bottom = space;
        //由于每行都只有column个，所以第一个都是column的倍数，把左边距设为0
        if (parent.getChildLayoutPosition(view) % column == 0 && excludeFirstSpace) {
            outRect.left = 0;
        }
    }
}
